package cn.gson.crm.model.service;


import cn.gson.crm.model.dao.ContractDao;
import cn.gson.crm.model.mapper.ContractMapper;
import cn.gson.crm.model.mapper.UserMapper;
import cn.gson.crm.model.pojos.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional(rollbackFor = Exception.class)
public class ContractService {
    @Autowired
    ContractMapper contractMapper;
    @Autowired
    ContractDao contractDao;
    @Autowired
    UserMapper userMapper;
    //通用方法查询当前登录人是否为老板
    public Integer fouyuan(HttpSession session){
        User login = (User)session.getAttribute("login");
        int ygId=login.getyId();
        //查询是否为老板
        List<User> userList=userMapper.selectctlaofou(ygId);
        List<Role> roleList=userList.get(0).getRoleList();
        for(int i=0;i<roleList.size();i++){
            if(roleList.get(i).getjZil()==1){
                return null;
            }
        }
        int ygId2= (int) login.getEmployees().get(0).getYgId();
        return ygId2;
    }
    //分页查询合同加条件负责人
    public  Map<String,Object> seleAll2emp(HttpSession session,Integer oursingner,Integer yid,String dept,Integer page,Integer size,String jinnian,String qunian,String qiannian){
        oursingner=fouyuan(session);
        List<Contract> contractList2=contractMapper.selectContrat(oursingner);
        Page<Object> obj = PageHelper.startPage(page, size);
        List<Contract> contractList=contractMapper.selectContrat(oursingner);
        //合同编号
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
        String newDate=sdf.format(new Date());
        String result="";
        Random random=new Random();
        for(int i=0;i<3;i++){
            result+=random.nextInt(10);
        }
        //新增审批批人下拉框
        List<Employee> employees=contractMapper.selectsp(dept);
        List<Employee> employeeList=contractMapper.selectLB();
        //统计图时间查询数据
        List<Contract> contractList4=contractMapper.selectnian(oursingner,jinnian);
        List<Contract> contractList5=contractMapper.selectnian(oursingner,qunian);
        List<Contract> contractList6=contractMapper.selectnian(oursingner,qiannian);
        User login = (User)session.getAttribute("login");

        int ygId= (int) login.getEmployees().get(0).getYgId();
        //查询当前登录人所负责的客户
        List<Customer> customerList=contractMapper.selectcustomer(ygId);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",contractList);
        map.put("tong",contractList2);
        map.put("number","HT"+newDate+result);
        map.put("employees",employees);
        map.put("employeeList",employeeList);
        map.put("customerList",customerList);
        map.put("jinli",employees);
        map.put("lb",employeeList);
        map.put("jinnian",contractList4);
        map.put("qunian",contractList5);
        map.put("qiannian",contractList6);
        return map;
    }

    //按条件查询合同记录
    public  Map<String,Object> selectContractWhere(HttpSession session,Integer oursingner,String condition,Integer page,Integer size){
        oursingner=fouyuan(session);
        Page<Object> obj = PageHelper.startPage(page, size);
        if(condition.equals(' ')||condition.isEmpty()){
            condition="null";
        }

        List<Contract> contractList=contractMapper.selectWhereContrat(oursingner,condition);
        //判断条件查询查到数据没，否则不按条件查询查询数据
        if(contractList==null||contractList.isEmpty()){
            Page<Object> obj2 = PageHelper.startPage(page, size);
            List<Contract> contractList2=contractMapper.selectContrat(oursingner);
            Map<String,Object> map = new HashMap<>();
            map.put("total",obj2.getTotal());
            map.put("rows",contractList2);
            return map;
        }else {
            Map<String, Object> map = new HashMap<>();
            map.put("total", obj.getTotal());
            map.put("rows", contractList);
            return map;
        }
    }
    //客户签约人按条件查询
    public List<Linkman> selectlinkman(Integer ji){
        List<Linkman> linkmen=contractMapper.selectqyr(ji);
        return linkmen;
    }
    //根据客户查询最新报价
    public List<Customer> selectbaojia(Integer cid){
        List<Customer> customerList=contractMapper.selectbaojia(cid);
        return customerList;
    }
    //新增合同
    public void saveContract(HttpSession session,Contract contract){
        if(contract.getContractId()>0){
            contractDao.save(contract);

        }else {
            contractDao.save(contract);
            Check check = new Check();
            User login = (User)session.getAttribute("login");
            int yname= (int) login.getEmployees().get(0).getYgId();
            check.setChecksqinName((long) yname);
            check.setCheckType("合同审批");
            check.setChecktypeid(contract.getContractId());
            check.setCheckDate(contract.getStarttime());
            check.setCheckName(contract.getCrApprover());
            check.setCheckCondition("未审核");
        }

    }
    // //根据客户查询所有没有签合同的销售机会
    public List<Sales> selectslase(Integer sid){
        return  contractMapper.selectslase(sid);
    }
    //新增之后此销售机会合同进行绑定-->
    public void updatxs(Integer cid,Integer sid){
        contractMapper.updatexs(cid,sid);
    }
    //根据合同状态查询合同记录
    public  Map<String,Object> selectzhutai(HttpSession session,Integer oursingner,String zhutai,Integer page,Integer size) {
        oursingner=fouyuan(session);
        if (zhutai.equals("全部合同")) {
            Page<Object> obj2 = PageHelper.startPage(page, size);
            List<Contract> contractList2=contractMapper.selectContrat(oursingner);
            Map<String,Object> map = new HashMap<>();
            map.put("total",obj2.getTotal());
            map.put("rows",contractList2);
            return map;
        } else {
            Page<Object> obj2 = PageHelper.startPage(page, size);
            List<Contract> contractList2=contractMapper.selectzhutai(oursingner, zhutai);
            Map<String,Object> map = new HashMap<>();
            map.put("total",obj2.getTotal());
            map.put("rows",contractList2);
            return map;
        }
    }
    //修改查询报价和审批人
    public Map<String,Object> selectxiuxiang(Integer sid,Integer cid){
        List<Customer> customerList=contractMapper.selectxiubao(sid);
        List<Employee> employeeList=contractMapper.selectxiusp(cid);
        Map<String,Object> map = new HashMap<>();
        map.put("sp",employeeList);
        map.put("baojia",customerList);
        return map;
    }
    //高级查询（多条件查询）
    public Map<String,Object> gaojiselect(HttpSession session,Integer oursingner,Integer condition1,String condition2,String condition3,String condition4,String condition5,String condition6,Integer page,Integer size){
        oursingner=fouyuan(session);
       System.out.println(condition2+"---------");
        System.out.println(condition6+"---------");

        Page<Object> obj2 = PageHelper.startPage(page, size);
        List<Contract> contractList2=contractMapper.gaojiselect(oursingner,condition1,condition2,condition3,condition4,condition5,condition6);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj2.getTotal());
        map.put("rows",contractList2);
        return map;
    }
    //主页统计合同
    public List<Shuliang> selectContrant(){
        List<Shuliang> list=contractMapper.selectcontract();
        System.out.println(list);
        return list;
    }
}
